<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="播种期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="timeOfSowing">
              <j-date placeholder="请选择播种期" v-model="model.timeOfSowing" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="播种量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="amountOfSeeding">
              <a-input v-model="model.amountOfSeeding" placeholder="请输入播种量"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="播种深度" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="depthOfSeeding">
              <a-input-number v-model="model.depthOfSeeding" placeholder="请输入播种深度" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="种苗" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="seedName">
              <j-dict-select-tag
                type="list"
                v-model="model.seedName"
                dictCode="zzzs_seedling_management,seeds_name,id"
                placeholder="请选择种苗"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="地块" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="fieldId">
              <j-dict-select-tag
                type="list"
                v-model="model.fieldId"
                dictCode="zzzs_dand_plot_data,name_of_land,id"
                placeholder="请选择地块"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否展示" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <a-switch
                checked-children="显示"
                un-checked-children="不显示"
                default-checked
                v-model="model.statusBoolean"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'ZzzsManagementOfSowingForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        timeOfSowing: [{ required: true, message: '请输入播种期!' }],
        amountOfSeeding: [{ required: true, message: '请输入播种量!' }],
        depthOfSeeding: [{ required: true, message: '请输入播种深度!' }],
        seedName: [{ required: true, message: '请输入种苗!' }],
        fieldId: [{ required: true, message: '请输入地块!' }],
      },
      url: {
        add: '/modules/zzzsManagementOfSowing/add',
        edit: '/modules/zzzsManagementOfSowing/edit',
        queryById: '/modules/zzzsManagementOfSowing/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (that.model.statusBoolean) {
          that.model.status = 1
        } else {
          that.model.status = 0
        }
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>